<template>
  <div class="formBox">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统设置</el-breadcrumb-item>
      <el-breadcrumb-item>暂存点管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <!-- <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>-->
        <el-col :span="4">
          <el-button type="primary" @click="goAdd">添加</el-button>
        </el-col>
      </el-row>

      <!-- table表格区域 -->
      <el-table :data="tableData" label-width="auto" fit size="medium">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="日期">
          <template slot-scope="scope">
            <span>{{scope.row.creationTime | relativeTime}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="houseName" label="暂存点ID"></el-table-column>
        <el-table-column prop="warehouseId" label="暂存点名字"></el-table-column>

        <el-table-column prop="houseName" label="编辑">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="goModify(scope.row.warehouseId,scope.row.houseName,scope.row.id)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="goDelete(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
    <!-- 弹出框 -->
    <el-dialog
      title="提示"
      :visible.sync="addCateDialogVisible"
      width="30%"
      @close="setRoleDialogClosed"
    >
      <el-form label-width="100px" class="demo-ruleForm" :model="addForm">
        <el-form-item label="暂存点ID：" prop="account">
          <el-input v-model="addForm.warehouseId"></el-input>
        </el-form-item>
        <el-form-item label="暂存点名字：" prop="password">
          <el-input v-model="addForm.houseName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addFormData">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 弹出框 -->
    <!-- 编辑弹出框 -->
    <el-dialog title="提示" :visible.sync="modifyCateDialogVisible" width="30%">
      <el-form label-width="100px" class="demo-ruleForm" :model="addForm">
        <el-form-item label="暂存点ID：" prop="account">
          <el-input v-model="modifydata.warehouseId"></el-input>
        </el-form-item>
        <el-form-item label="暂存点名字：" prop="password">
          <el-input v-model="modifydata.houseName"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="modifyCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="modifyFormData">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑弹出框 -->
  </div>
</template>

<script>
import { addStaging, getData, modifyData } from '../network/api.js'
export default {
  name: 'ss',
  data() {
    return {
      total: 0,
      // 获取用户列表的参数对象
      queryInfo: {
        query: '',
        // 当前的页数
        pagenum: 1,
        // 当前每页显示多少条数据
        pagesize: 2,
      },
      addForm: {
        warehouseId: '',
        houseName: '',
      },
      // 添加表单的验证规则对象
      // 添加表单的验证规则对象
      // addFormRules: {
      //   account: [
      //     { required: true, message: '请输入暂存点ID', trigger: 'blur' },
      //   ],
      //   password: [
      //     { required: true, message: '请输入暂存点名字', trigger: 'blur' },
      //   ],
      // },
      // 编辑弹框阀
      modifyCateDialogVisible: false,
      // diolog框开启关闭阀
      addCateDialogVisible: false,

      // 查询参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      // 数据列表
      tableData: [],

      // 编辑弹窗的数据
      modifydata: {
        warehouseId: '',
        houseName: '',
        id: '',
      },
    }
  },
  created() {
    this.getFormData()
  },
  methods: {
    // 点击添加按钮
    goAdd() {
      this.addCateDialogVisible = true
    },
    // 点击弹窗的确定安妮
    async addFormData() {
      console.log(123)
      this.addCateDialogVisible = false
      addStaging({
        warehouseId: this.addForm.warehouseId,
        houseName: this.addForm.houseName,
      })
        .then((res) => {
          console.log(res)
          this.getFormData()
        })
        .catch((err) => {
          console.log(err)
        })
    },
    async getFormData() {
      await getData({
        SkipCount: (this.queryInfo.pagenum - 1) * this.queryInfo.pagesize,
        MaxResultCount: this.queryInfo.pagesize,
      })
        .then((res) => {
          // console.log(res)
          // console.log(res.result.items)
          this.tableData = res.result.items
          this.tableData.creationTime
          // console.log(res.result.totalCount)
          this.total = res.result.totalCount
        })
        .catch((err) => {
          console.log(err)
        })
    },

    // 编辑确定按钮
    async modifyFormData() {
      this.modifyCateDialogVisible = false
      await modifyData({
        warehouseId: this.modifydata.warehouseId,
        houseName: this.modifydata.houseName,
        id: this.modifydata.id,
      })
        .then((res) => {
          console.log(res)
          this.getFormData()
        })

        .catch((err) => {
          console.log(err)
        })
    },
    // 编辑按钮
    goModify(nameId, space, id) {
      this.modifyCateDialogVisible = true
      // console.log(id, 222)
      // console.log(space, 222)
      ;(this.modifydata.warehouseId = nameId),
        (this.modifydata.houseName = space)
      this.modifydata.id = id
    },
    async goDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$axios
            .delete(
              'http://39.104.74.183:21028/api/services/app/Med_Warehouse/Delete?id=' +
                id +
                ''
            )
            .then((res) => {
              // console.log(res.status)
              this.getFormData()
            })
            .catch((err) => {
              console.log(err)
            })
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
      // console.log(id)
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getFormData()
    },

    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getFormData()
    },

    // 监听对话框的关闭事件，重置表单数据
    // 监听分配角色对话框的关闭事件
    setRoleDialogClosed() {
      this.addForm.warehouseId = ''
      this.addForm.houseName = ''
    },
  },
}
</script>

<style lang="less" scoped>
.formBox {
  width: 100%;
  height: 100%;
  // background-color: pink;
  .el-table {
    width: 100%;
    height: 100%;
    .el-table-column {
      justify-content: space-around !important;
    }
  }
}
</style>
